﻿scrX, scrY = guiGetScreenSize ()
x = scrX/2 - 944/2
y = scrY - 636


-- Private messages --
function pmBuild()
	local gui = {}
	gui._placeHolders = {}
	
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 728, 472
	local left = x+114
	local top = y+57
	gui["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight, "Komunikator", false)
	guiWindowSetSizable(gui["_root"], false)
	guiWindowSetMovable (gui["_root"], false)
	
	gui["plainTextEdit"] = guiCreateMemo(220, 25, 501, 401, "", false, gui["_root"])
	
	gui["listWidget"] = guiCreateGridList(9, 24, 201, 431, false, gui["_root"])
	guiGridListSetSortingEnabled(gui["listWidget"], false)
	gui["listWidget_col"] = guiGridListAddColumn(gui["listWidget"], "Gracze", 0.85)
	local listWidget_row = nil
	
	gui["lineEdit"] = guiCreateEdit(220, 435, 411, 20, "", false, gui["_root"])
	guiEditSetMaxLength(gui["lineEdit"], 32767)
	
	gui["pushButton"] = guiCreateButton(640, 435, 75, 23, "Wyślij", false, gui["_root"])
	gui["closeButton"] = guiCreateButton(left, top, 20, 20, "X", false)			
	guiSetVisible(gui["_root"], false)
	guiSetVisible(gui["closeButton"], false)
	guiSetProperty ( gui["closeButton"], "AlwaysOnTop", "True" ) -- trollface.jpg
	
		-- Build gridlist :C --
	for id, player in ipairs(getElementsByType("player")) do
		local row = guiGridListAddRow ( gui["listWidget"] )
		guiGridListSetItemText ( gui["listWidget"], row, gui["listWidget_col"], getPlayerName ( player ), false, false )
	end
	outputDebugString(tostring(guiSetInputMode("no_binds_when_editing")))
	
	return gui, windowWidth, windowHeight
end
local pmWindow = pmBuild()

function handlePM()
	guiSetVisible(pmWindow["_root"], true)
	guiSetVisible(pmWindow["closeButton"], true)
	guiBringToFront(pmWindow["_root"])
	guiBringToFront(pmWindow["closeButton"])
	guiSetInputMode("no_binds_when_editing")
end

addEventHandler("onClientGUIClick", pmWindow["closeButton"], function()
	if guiGetVisible(pmWindow["_root"]) then
		guiSetVisible(pmWindow["_root"], false)
		guiSetVisible(pmWindow["closeButton"], false)
	end
end, false)

addEventHandler("onClientGUIClick", pmWindow["pushButton"], function()
	if guiGetVisible(pmWindow["_root"]) then
		local row, col = guiGridListGetSelectedItem (pmWindow["listWidget"])
		if row == -1 then outputChatBox("* Nie zaznaczyłeś gracza!", 255,0,0) return end
		local plName = guiGridListGetItemText ( pmWindow["listWidget"], row, col )
		--if plName == getPlayerName(localPlayer) then outputChatBox("* Nie możesz wysłać wiadomości do siebie!", 255,0,0) return end
		local msg = guiGetText (pmWindow["lineEdit"])
		triggerServerEvent ( "onPlayerPrivateChat", localPlayer, plName, msg )
	end
end, false)

-- End of Private messages --